package cn.powertime.evaluation.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @author ZYW
 */
@Component
public class SMSCodeUtils {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 发送短信验证码
     * @param smsCode 要发送的验证码
     * @return 返回发送是否成功
     */
    public boolean sendSmsCode(String smsCode) {
        //TODO 发送验证码逻辑
        return true;
    }

    /**
     * 验证短信验证码是否输入正确
     * @param smsCode 短信验证码
     * @return 返回是否存在
     */
    public int verifySMSCode(String smsCode) {
        Boolean hasKey = redisTemplate.hasKey(smsCode);
        if(hasKey) {
            String time = redisTemplate.boundValueOps(smsCode).get();
            long millis = System.currentTimeMillis();
            assert time != null;
            if(millis > Long.parseLong(time)) {
                return 0;
            }
            return 1;
        }

        return -1;
    }
}
